package de.backtomedieval.activities.gamemenu;

import de.backtomedieval.activities.TopBar;
import de.backtomedieval.activities.startscreen.ProfileStartscreen;
import de.backtomedieval.activities.startscreen.Startscreen;
import de.backtomedieval.game.Fight;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import de.backtomedieval.R;
import de.backtomedieval.main.Game;


public class FightScreen extends Activity {
	
	private Handler mHandler = new Handler();

	private TextView fightInfo;
	private int refresh = Game.getRefreshrate();
	private TopBar topbar;
	
	private boolean fightOver = false;
	

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("Open fights:",""+ Fight.getFights().size());
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
        setContentView(R.layout.fight); 
		fightInfo = (TextView) findViewById(R.id.fightinfo);
	    fightInfo.setText("");
	    
		topbar = new TopBar(this);
		topbar.setTitle(Game.getGame().getcCity().getName() + ": "+ getString(R.string.fight));	
	   
		mHandler.removeCallbacks(mUpdateFightTask);
		mHandler.postDelayed(mUpdateFightTask, 100);
    } 
    
    
    
	private Runnable mUpdateFightTask = new Runnable() {
		   public void run() {
			   mHandler.postDelayed(mUpdateFightTask, refresh);
			   topbar.refreshCityInfo();
			   if(!fightOver) {
				   if (!Fight.getFights().isEmpty()) {
						  int id= Fight.getFights().size()-1;
						  fightInfo.setText(Fight.getFights().get(id).getFightString());
						  if (Fight.getFights().get(id).isFightOver()) {
							 fightOver=true;
						  }
					   } else {
						   fightInfo.setText("No fight at this moment.");
						   fightOver=true;
					   }
			   }
			   
		   }
	};
	
	@Override
	public void onBackPressed() {
		closeFight();
	}
	
	private void closeFight() {
		int id= Fight.getFights().size()-1;
		if (Fight.getFights().size()>0) {
			
			if (Fight.getFights().get(id).isFinalFight() && !Fight.getFights().get(id).isFightOver()) {
				Game.showPopup(this, getString(R.string.cannotleaveinfight));
				return;
			}
		}
		Intent changeToIntent = new Intent(FightScreen.this,Gamemenu.class);
		if (id!=-1) {
			//Fight lastFight = Fight.getFights().get(id);
			if (!Game.getGame().isGameActive()) {
				changeToIntent = new Intent(FightScreen.this,ProfileStartscreen.class);
			}
		}
		startActivity(changeToIntent);
		for (int i=0; i<Fight.getFights().size();i++) {
			if (Fight.getFights().get(i).isFightOver()) {
				Fight.removeFight(i);
				i--;
			}
		}
		this.finish();
	}

	@Override
	public void onResume(){
	   super.onResume();
	   Game.setLastActivity(this);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		
	}

	@Override
	public void onPause(){
	   super.onPause();
	   mHandler.removeCallbacks(mUpdateFightTask);
	}
	
	
}
